global parity
global G
global H
global errTable
global n, 
global k
global n_k;

parity = [0 1 1; 
          1 0 1; 
          1 1 1; 
          1 1 0 ];
G = [eye(4,4)  parity];
H = [parity; eye(3,3)];

errTable = syndtable(H');
n = 7;
k = 4;
n_k = 3;

function c = linearEncode(m)
  global G;
  c = modulo(m*G,2);
endfunction

function m = linearDecode(c)
  global H, errTable, k;
  len = size(c,1)
  m = zeros(len , k)
  for j = 1:len
    row = c(j, :);
    s = modulo(row * H,2);
    err = errTable(bi2de(fliplr(s))+1, :);
    row = modulo(err + row,2)
    m(j, :) = row(1:k);
  end
endfunction

  msg = round(rand(1,4));
  //msg = [1 0 0 1]
  code = linearEncode(msg);
  rcv_code = fixErrorNumber(code, 1);
  rcv_msg = linearDecode(rcv_code);
  length(find(msg ~= rcv_msg))

  

